package com.demo.datastructure.reorder.insertionSort;

/**
 * @description: 插入排序
 * @author: ljr
 * @time: 2021/5/2 16:32
 */
public class MyInsertionSort4 {

    // 插入排序，a表示数组，n表示数组大小
    public static void insertionSort(int[] args, int n) {
         for(int i = 0, j = i; i < n-1; j = ++i){
             int val = args[i+1];
             while(val < args[j]){
                 args[j+1] = args[j];
                 if(j-- == 0){
                     break;
                 }
             }
             args[j+1] = val;
         }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3,3,2,5,1,6};
        MyInsertionSort4.insertionSort(nums,6);
        for(int num : nums){
            System.out.println(num);
        }
    }

}
